//
//  ExamineDetailsViewController.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/8/26.
//

#import "ExamineDetailsViewController.h"
#import "ExamineDetailsFooterView.h"

#import "joinQuestListModel.h"
#import "ExamineDetailsTableViewCell.h"
#import "ExamineDetailsFooterView.h"

#import "SYAudio.h"
#import "YTTipsShowView.h"
#import "DescriptionShowView.h"

@interface ExamineDetailsViewController ()

@property (nonatomic ,strong) ExamineDetailsModel * model;
@property (nonatomic ,strong) UIView * buttonview;
@property (nonatomic ,strong) UIButton * leftBtn;
@property (nonatomic ,strong) UIButton * rightBtn;

@end

@implementation ExamineDetailsViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    UIView * topview = [UIView new];
    topview.backgroundColor = KcolorHex(@"#0967FF");
    [self.view addSubview:topview];
    [topview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.offset(0);
        make.height.offset(Knav_HeightX);
    }];
    MJWeakSelf
    UIView * backview = [UIView new].addClickBlock(^{
        [weakSelf popViewController];
    });
    [topview addSubview:backview];
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(44);
        make.left.bottom.offset(0);
        make.width.offset(kwt(170));
    }];
    
    UIImageView * leftimg = kimageView(@"white_path");
    [backview addSubview:leftimg];
    [leftimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(20);
        make.left.offset(kwt(10));
        make.centerY.offset(0);
    }];
    
    UILabel * leftlab = [DTKTool labelWithText:@"审核详情" andFont:kCFont(19) andTextColor:UIColor.whiteColor];
    [backview addSubview:leftlab];
    [leftlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(43));
        make.centerY.offset(0);
    }];
    
    
    UIView * buttonview = [UIView new];
    self.buttonview= buttonview;
    buttonview.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:buttonview];
    [buttonview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.height.offset(kwt(58) + KSafeAreaBottomHeight);
    }];
    
    UIView * line = [UIView new];
    line.backgroundColor = KcolorHex(@"#ECECEC");
    [buttonview addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.offset(0);
        make.height.offset(0.5);
    }];
    
    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.rightBtn = rightBtn;
    rightBtn.backgroundColor = [UIColor gradientColorWithSize:CGSizeMake(kwt(93), 1) direction:GradientColorDirectionLevel startColor:KcolorHex(@"#2892FE") endColor:KcolorHex(@"#0366FE")];
    [rightBtn setTitle:@"同意" forState:UIControlStateNormal];
    [rightBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    rightBtn.titleLabel.font = kCFont(16);
    [DTKTool YJ:kwt(5) andView:rightBtn];
    [rightBtn addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [buttonview addSubview:rightBtn];
    [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(kwt(36));
        make.width.offset(kwt(93));
        make.right.offset(kwt(-12));
        make.top.offset(kwt(11));
    }];
    
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.backgroundColor = UIColor.whiteColor;
    [leftBtn setTitle:@"拒绝" forState:UIControlStateNormal];
    [leftBtn setTitleColor:KcolorHex(@"#222222") forState:UIControlStateNormal];
    leftBtn.titleLabel.font = kFont(16);
    [DTKTool YJBK:kwt(5) andView:leftBtn andWidth:0.5 andColor:KcolorHex(@"#CDCDCD")];
    self.leftBtn = leftBtn;
    [leftBtn addTarget:self action:@selector(leftBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [buttonview addSubview:leftBtn];
    [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(kwt(36));
        make.width.offset(kwt(93));
        make.right.offset(kwt(-120));
        make.top.offset(kwt(11));
    }];
    
    
    
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(topview.mas_bottom);
        make.bottom.equalTo(buttonview.mas_top);
    }];
    [self loadNewData];
}
- (void)loadNewData{
    [self.view showBusyHUD];
    [SCNetManger PostAuditGetAuditDetailWithauditId:self.auditId andCompetionHandlerBlock:^(HTTPDicBase * model) {
        [self.view hideBusyHUD];
        if(model.code == 200){
            
            self.model = [ExamineDetailsModel yy_modelWithDictionary:model.result];
            if(self.model.button == 0){
                [self.buttonview mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.offset(0);
                }];
            } else {
                [self.buttonview mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.offset(kwt(58) + KSafeAreaBottomHeight);
                }];
                if(self.model.button == 1){//////按钮类型;1:撤销
                    self.leftBtn.hidden = YES;
                    [self.rightBtn setTitle:@"撤销" forState:UIControlStateNormal];
                } else  if(self.model.button == 2){// 2:同意 拒绝
                    
                }
            }
            [self.tableview reloadData];
            MJWeakSelf
            self.model.openDurationBlock = ^(NSString * _Nonnull voiceDuration) {
                [weakSelf.tableview reloadData];
            };
        } else {
            [self.view showWarning:model.message];
        }
    }];
}

- (void)leftBtnClick{
    ///审批拒绝
    MJWeakSelf
    [DescriptionShowView showViewWithTitle:@"拒绝申请" andContent:@"拒绝原因" andBlock:^(NSString * _Nonnull str) {
        [self.view showBusyHUD];
        NSMutableDictionary * dict = [NSMutableDictionary dictionary];
        [dict setObject:weakSelf.model.ID forKey:@"id"];
        [dict setObject:str forKey:@"reason"];
        [SCNetManger PostAuditAuditRefuseWithDict:dict andCompetionHandlerBlock:^(HTTPArrBase * model) {
            [self.view hideBusyHUD];
            if(model.code == 200){
                [self.view showWarning:model.message];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    // 2秒后执行的代码
                    [self loadNewData];
                });
            } else {
                [self.view showWarning:model.message];
            }
        }];
    }];;
    
}
- (void)rightBtnClick{
    MJWeakSelf
    [YTTipsShowView showviewWithTips:(self.model.button == 1)?@"确定撤销申请?":@"确定同意申请?" andRightBlock:^{
        
        if(self.model.button == 1){
            ///撤销
            [weakSelf.view showBusyHUD];
            [SCNetManger PostAuditCancelApplyWithauditId:weakSelf.model.ID andCompetionHandlerBlock:^(HTTPDicBase * model) {
                [weakSelf.view hideBusyHUD];
                if(model.code == 200){
                    [weakSelf.view showWarning:model.message];
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        // 2秒后执行的代码
                        [weakSelf loadNewData];
                    });
                } else {
                    [weakSelf.view showWarning:model.message];
                }
            }];
        } else if(self.model.button == 2){
            
            ///确认接受
            [self.view showBusyHUD];
            NSMutableDictionary * dict = [NSMutableDictionary dictionary];
            [dict setObject:weakSelf.model.ID forKey:@"id"];
            [SCNetManger PostAuditAuditAgreeWithDict:dict andCompetionHandlerBlock:^(HTTPArrBase * model) {
                [weakSelf.view hideBusyHUD];
                if(model.code == 200){
                    [weakSelf.view showWarning:model.message];
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        // 2秒后执行的代码
                        [weakSelf loadNewData];
                    });
                } else {
                    [weakSelf.view showWarning:model.message];
                }
                
            }];
            
        }
    }];
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    ExamineDetailsFooterView * footerview = [ExamineDetailsFooterView NewView];
    [footerview isSH];
    footerview.handoverProcessModel = self.model.handoverProcessModel;
    return footerview;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return self.model.JJFooterH;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return  1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.model.JJcellH;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    ExamineDetailsTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ExamineDetailsTableViewCell"];
    if (cell == nil) {
        cell = [[ExamineDetailsTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ExamineDetailsTableViewCell"];
    }
    cell.model = self.model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell ;
    
}
- (void)dealloc{
    [[SYAudio shareAudio] audioStop];
}
@end
